package com.shanghaionstar.activity.webview.handler.picture;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片查找辅助类 封装图片查找相关操作
 *
 * @author dc
 */
public class FindImageHelper {
    private Context mContext;

    public FindImageHelper(Context context) {
        this.mContext = context;
    }

    /**
     * @param path
     * @return
     */
    public long queryImageDateTaken(String path) {
        // mContext.sendBroadcast(new Intent(
        // Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"
        // + Environment.getExternalStorageDirectory())));// 更新扫描整个SD卡
        // // 4.4系统不可用
        //
        // Cursor mCursor = mContext.getContentResolver().query(
        // MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        // new String[] { MediaStore.Images.Media.DATA,
        // MediaStore.Images.Media.DATE_TAKEN }, null, null, null);
        // long dateTaken = 0;
        // if (mCursor != null) {
        // while (mCursor.moveToNext()) {
        // String data = mCursor.getString(mCursor
        // .getColumnIndex(MediaStore.Images.Media.DATA));
        // dateTaken = mCursor.getLong(mCursor
        // .getColumnIndex(MediaStore.Images.Media.DATE_TAKEN));
        // }// end while
        // mCursor.close();
        // }
        // ExifInterface intf = null;
        // try {
        // intf = new ExifInterface(path);
        // } catch (IOException e) {
        // e.printStackTrace();
        // }

        // if (intf != null) {
        // String dateString = intf.getAttribute(ExifInterface.TAG_DATETIME);
        // }

        return 0;
    }

    /**
     * 返回指定文件夹下所有图片列表,为空则获取全部文件
     *
     * @param folderId
     * @return /** Cursor cur = getActivity().getContentResolver()
     * <p/>
     * <p/>
     * final List<GridItem> images = new ArrayList <GridItem>(cur.getCount());
     * <p/>
     * if (cur != null) { if (cur.moveToFirst()) { while (!cur.isAfterLast()) { images.add(new
     * GridItem(cur.getString(1), cur.getString(0),cur.getString(2),cur.getLong(3))); cur.moveToNext(); } }
     * cur.close(); }
     */
    public List<ImageDetailBean> findImagesByFolder(String folderId) {
        List<ImageDetailBean> picList = new ArrayList<ImageDetailBean>();
        Cursor mCursor = null;
        if (!TextUtils.isEmpty(folderId)) {
            mCursor = mContext.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    null,
                    MediaStore.Images.Media.BUCKET_ID + " = ? AND " + MediaStore.Images.Media.SIZE + " > 0",
                    new String[]{folderId},
                    MediaStore.Images.Media.DATE_MODIFIED + " ASC");
        } else {
            try {
                mCursor = mContext.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        null,
                        MediaStore.Images.Media.SIZE + " > 0",
                        null,
                        MediaStore.Images.Media.DATE_MODIFIED + " ASC");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (mCursor != null) {
            while (mCursor.moveToNext()) {
                // 获取图片的路径
                String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA));
                long dateTaken = mCursor.getLong(mCursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN));
                int size = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Images.Media.SIZE));
                ImageDetailBean addItem = new ImageDetailBean();
                addItem.setImage(path);
                addItem.setDateTaken(dateTaken);
                addItem.setSize(size);
                picList.add(addItem);
            } // end while
            mCursor.close();
        }
        return picList;
    }

}// end class
